import 'dart:io';
import 'package:flutter/services.dart';

class DirectoryAccessService {
  static const MethodChannel _channel = MethodChannel('directory_access');
  
  /// 选择目录并获取访问权限
  static Future<String?> selectDirectoryWithAccess() async {
    try {
      final String? result = await _channel.invokeMethod('selectDirectory');
      return result;
    } on PlatformException catch (e) {
      print('选择目录失败: ${e.message}');
      return null;
    }
  }
  
  /// 检查是否有目录访问权限
  static Future<bool> hasDirectoryAccess(String path) async {
    try {
      final bool result = await _channel.invokeMethod('hasDirectoryAccess', {'path': path});
      return result;
    } on PlatformException catch (e) {
      print('检查目录访问权限失败: ${e.message}');
      return false;
    }
  }
  
  /// 开始访问目录（使用安全作用域书签）
  static Future<bool> startAccessingDirectory(String path) async {
    try {
      final bool result = await _channel.invokeMethod('startAccessingDirectory', {'path': path});
      return result;
    } on PlatformException catch (e) {
      print('开始访问目录失败: ${e.message}');
      return false;
    }
  }
  
  /// 停止访问目录
  static Future<void> stopAccessingDirectory(String path) async {
    try {
      await _channel.invokeMethod('stopAccessingDirectory', {'path': path});
    } on PlatformException catch (e) {
      print('停止访问目录失败: ${e.message}');
    }
  }
  
  /// 保存目录书签
  static Future<bool> saveDirectoryBookmark(String path) async {
    try {
      final bool result = await _channel.invokeMethod('saveDirectoryBookmark', {'path': path});
      return result;
    } on PlatformException catch (e) {
      print('保存目录书签失败: ${e.message}');
      return false;
    }
  }
  
  /// 恢复目录书签访问权限
  static Future<bool> restoreDirectoryBookmark(String path) async {
    try {
      final bool result = await _channel.invokeMethod('restoreDirectoryBookmark', {'path': path});
      return result;
    } on PlatformException catch (e) {
      print('恢复目录书签失败: ${e.message}');
      return false;
    }
  }
}